package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.ImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("ordersetting")
public class OrderSettingController {

    @DubboReference
    private OrderSettingService orderSettingService;

    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            return new Result(false, "文件名称不能为空");
        }
        List<List<Object>> lists;
        try {
            if (originalFilename.endsWith(".xlsx")) {
                lists = ImportUtil.read2007Excel(multipartFile.getInputStream());
            } else if (originalFilename.endsWith(".xls")) {
                lists = ImportUtil.read2003Excel(multipartFile.getInputStream());
            } else {
                return new Result(false, "非法文件名称");
            }
            //将lists转换成list<ordersetting>
            List<OrderSetting> orderSettingList = toListordersetting(lists);
            orderSettingService.add(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConst.UPLOAD_SUCCESS);
    }

    private List<OrderSetting> toListordersetting(List<List<Object>> lists) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OrderSetting> orderSettingList1 = new ArrayList<>();
        for (int i = 1; i < lists.size(); i++) {//跳过第一行表头
            List<Object> cells = lists.get(i);//不建议遍历行获取单元格，因为可能有脏数据
            String orderDate = (String) cells.get(0);
            String num = (String) cells.get(1);
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(sdf.parse(orderDate));
            orderSetting.setNumber(Integer.parseInt(num));
            orderSettingList1.add(orderSetting);
        }

        return orderSettingList1;
    }

    @GetMapping("findByDate")
    public Result findByDate(String startTime, String endTime) {
        List<OrderSetting> orderSettingList = orderSettingService.findByDate(startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            Date orderDate = orderSetting.getOrderDate();
            calendar.setTime(orderDate);
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return new Result(true, MessageConst.ACTION_SUCCESS, list);
    }

    @PostMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true, MessageConst.ACTION_SUCCESS);

    }

}
